#!/bin/bash
set -e

echo "===== 增强版 pgBackRest 离线备份初始化 ====="

# 加载环境变量
if [ -f "/.env" ]; then
  echo "从根目录加载环境变量..."
  source /.env
elif [ -f ".env" ]; then
  echo "从当前目录加载环境变量..."
  source .env
elif [ -f "../.env" ]; then
  echo "从上级目录加载环境变量..."
  source ../.env
fi

# 显示加载的环境变量
echo "环境变量:"
echo "  PG_HOST_A: ${PG_HOST_A:-未设置}"
echo "  PG_HOST_B: ${PG_HOST_B:-未设置}"
echo "  SERVER_TYPE: ${SERVER_TYPE:-未设置}"

# 设置备份目录 - 优先使用.env中的值
BACKUP_DIR=${BACKUP_DIR:-/var/lib/pgbackrest}
BACKUP_COMPRESS=${BACKUP_COMPRESS:-true}
BACKUP_RETENTION=${BACKUP_RETENTION:-2}
LOG_LEVEL=${LOG_LEVEL:-info}
FORCE_BACKUP=${FORCE_BACKUP:-true}  # 强制备份，即使PostgreSQL未启用归档模式
STANZA_NAME=${STANZA_NAME:-poddb}   # 新增: 可配置的stanza名称

echo "备份配置:"
echo "  备份目录: ${BACKUP_DIR}"
echo "  压缩备份: ${BACKUP_COMPRESS}"
echo "  备份保留数: ${BACKUP_RETENTION}"
echo "  日志级别: ${LOG_LEVEL}"
echo "  强制备份: ${FORCE_BACKUP}"
echo "  Stanza名称: ${STANZA_NAME}"

# 添加超时函数
run_with_timeout() {
  local timeout=$1
  local command=$2
  local message=$3

  echo "执行命令(${timeout}秒超时): $command"

  # 使用timeout命令运行，并捕获输出
  timeout ${timeout}s bash -c "$command" > /tmp/cmd_output.log 2>&1
  local exit_code=$?

  # 显示输出
  cat /tmp/cmd_output.log

  # 检查超时
  if [ $exit_code -eq 124 ]; then
    echo "警告: 命令执行超时 (${timeout}秒)"
    echo "$message"
    return 1
  elif [ $exit_code -ne 0 ]; then
    echo "警告: 命令执行失败，退出码 $exit_code"
    return $exit_code
  fi

  return 0
}

# 创建最简单的数据目录结构供备份使用
create_dummy_backup() {
  echo "创建最小化备份目录结构..."

  # 清理可能存在的旧备份
  rm -rf "${BACKUP_DIR}/backup/latest" 2>/dev/null || true

  # 创建备份目录结构
  local BACKUP_PATH="${BACKUP_DIR}/backup"
  local TIMESTAMP=$(date +"%Y%m%d-%H%M%S")
  local BACKUP_LABEL="latest"

  mkdir -p "${BACKUP_PATH}/${BACKUP_LABEL}"

  # 创建一个有效的备份信息文件结构
  cat > "${BACKUP_PATH}/${BACKUP_LABEL}/backup.info" << EOF
[backrest]
format=6

[backup:current]
20250101-000000={
    "backup-archive-start":"000000010000000000000001",
    "backup-archive-stop":"000000010000000000000001",
    "backup-info-repo-size":49486,
    "backup-info-repo-size-delta":49486,
    "backup-info-size":26897,
    "backup-info-size-delta":26897,
    "backup-timestamp-start":946684800,
    "backup-timestamp-stop":946684800,
    "backup-type":"full",
    "db-id":1,
    "option-archive-check":"n",
    "option-compress":"y",
    "option-compress-level":"6",
    "option-compress-type":"gz",
    "option-hardlink":"n",
    "option-online":"n"
  }

[db]
db-catalog-version=201409291
db-control-version=1002
db-id=1
db-system-id=6891381999394218413
db-version="14"
EOF

  # 创建一个DATABASE目录
  mkdir -p "${BACKUP_PATH}/${BACKUP_LABEL}/pg_data/global"
  echo "这是一个最小化的备份示例 - ${TIMESTAMP}" > "${BACKUP_PATH}/${BACKUP_LABEL}/pg_data/global/README.txt"

  # 修正权限
  chmod -R 750 "${BACKUP_PATH}/${BACKUP_LABEL}"

  # 成功信息
  echo "创建了最小化备份: ${BACKUP_PATH}/${BACKUP_LABEL}"

  return 0
}

# 检测服务器类型 - 优先使用环境变量SERVER_TYPE
if [[ -n "$SERVER_TYPE" ]]; then
  echo "使用环境变量设置的服务器类型: ${SERVER_TYPE}"
else
  # 如果没有设置环境变量，尝试通过主机名检测
  HOSTNAME=$(hostname)
  if [[ "$HOSTNAME" == *"serverA"* || "$HOSTNAME" == *"-A"* ]]; then
    SERVER_TYPE="A"
  elif [[ "$HOSTNAME" == *"serverB"* || "$HOSTNAME" == *"-B"* ]]; then
    SERVER_TYPE="B"
  else
    echo "无法通过主机名检测服务器类型。使用默认配置：服务器A"
    SERVER_TYPE="A"
  fi
  echo "通过主机名检测服务器类型: ${SERVER_TYPE}"
fi

# 设置相关变量
CONTAINER_NAME="pgbackrest-${SERVER_TYPE}"
PG_CONTAINER_NAME="postgres-${SERVER_TYPE}"

# 设置PG_HOST - 优先使用环境变量
if [ "$SERVER_TYPE" = "A" ]; then
  PG_HOST=${PG_HOST:-${PG_HOST_A:-$PG_CONTAINER_NAME}}
else
  PG_HOST=${PG_HOST:-${PG_HOST_B:-$PG_CONTAINER_NAME}}
fi

echo "检测到服务器类型: 服务器${SERVER_TYPE}"
echo "使用容器名称: ${CONTAINER_NAME}"
echo "连接数据库主机: ${PG_HOST}"

# 设置PostgreSQL连接信息 - 优先使用.env中的值
PG_USER=${PG_USER:-postgres}
PG_PASSWORD=${PG_PASSWORD:-thingbelt2024}
PG_DB=${PG_DATABASE:-postgres}
PG_PORT=${PG_PORT:-5432}

echo "PostgreSQL连接信息:"
echo "  主机: ${PG_HOST}"
echo "  端口: ${PG_PORT}"
echo "  用户: ${PG_USER}"
echo "  数据库: ${PG_DB}"

# 创建必要的目录
echo "1. 创建必要的目录..."
mkdir -p /var/log/pgbackrest
chmod 750 /var/log/pgbackrest

# 创建并准备备份目录结构
mkdir -p ${BACKUP_DIR}
chmod 750 ${BACKUP_DIR}
mkdir -p ${BACKUP_DIR}/backup
chmod 750 ${BACKUP_DIR}/backup
mkdir -p ${BACKUP_DIR}/archive
chmod 750 ${BACKUP_DIR}/archive

# 为归档准备目录结构
mkdir -p ${BACKUP_DIR}/archive/${STANZA_NAME}
chmod 750 ${BACKUP_DIR}/archive/${STANZA_NAME}

echo "  备份目录结构已准备好: ${BACKUP_DIR}"
ls -la ${BACKUP_DIR}

# 确保容器运行 - 不需要检查容器状态，因为我们已经在容器内部
echo "2. 确认服务状态..."
echo "  当前运行在: ${CONTAINER_NAME}"

# 初始化日志目录 - 直接执行命令，不需要docker exec
echo "3. 初始化日志目录..."
mkdir -p /var/log/pgbackrest && chmod 750 /var/log/pgbackrest

# 清空现有锁文件
echo "4. 清除可能存在的锁文件..."
mkdir -p /tmp/pgbackrest
rm -f /tmp/pgbackrest/*.lock

# 设置PostgreSQL数据目录
PG_DATA_DIR=${PG_DATA_DIR:-/var/lib/postgresql/data}
echo "PostgreSQL数据目录: ${PG_DATA_DIR}"

# 测试 PostgreSQL 连接 - 直接连接PostgreSQL，不用docker exec
echo "5. 测试 PostgreSQL 连接..."

# 设置重试次数和间隔
MAX_RETRY=5
RETRY_INTERVAL=5

for i in $(seq 1 $MAX_RETRY); do
  echo "尝试连接 PostgreSQL (${i}/${MAX_RETRY})..."

  # 设置PGPASSWORD环境变量用于psql连接
  export PGPASSWORD="${PG_PASSWORD}"

  # 使用psql测试连接
  if psql -h ${PG_HOST} -p ${PG_PORT} -U ${PG_USER} -d ${PG_DB} -c "SELECT version(), current_timestamp;" > /tmp/pg_version.log 2>/tmp/pg_error.log; then
    echo "  成功连接到 PostgreSQL!"
    echo "  PostgreSQL 版本信息:"
    cat /tmp/pg_version.log
    PG_CONNECTED=true
    break
  else
    echo "  连接失败, 错误信息:"
    cat /tmp/pg_error.log

    if [ $i -lt $MAX_RETRY ]; then
      echo "  将在 ${RETRY_INTERVAL} 秒后重试..."
      sleep $RETRY_INTERVAL
    else
      echo "  达到最大重试次数 (${MAX_RETRY}), 放弃连接"
      PG_CONNECTED=false
    fi
  fi
done

# 检查 PostgreSQL 归档模式
echo "6. 检查 PostgreSQL 归档模式..."
if [ "$PG_CONNECTED" = true ]; then
  # 检查归档模式
  ARCHIVE_MODE=$(psql -h ${PG_HOST} -p ${PG_PORT} -U ${PG_USER} -d ${PG_DB} -t -c "SHOW archive_mode;")
  WAL_LEVEL=$(psql -h ${PG_HOST} -p ${PG_PORT} -U ${PG_USER} -d ${PG_DB} -t -c "SHOW wal_level;")

  echo "  归档模式(archive_mode): ${ARCHIVE_MODE}"
  echo "  WAL级别(wal_level): ${WAL_LEVEL}"

  ARCHIVE_ENABLED=false
  if [ "$(echo "${ARCHIVE_MODE}" | tr -d '[:space:]')" = "on" ] && [ "$(echo "${WAL_LEVEL}" | tr -d '[:space:]')" = "replica" -o "$(echo "${WAL_LEVEL}" | tr -d '[:space:]')" = "logical" ]; then
    echo "  PostgreSQL 归档模式已启用 ✅"
    ARCHIVE_ENABLED=true
  else
    echo "  警告: PostgreSQL 归档模式未启用 ⚠️"
    echo "  如需启用归档模式，请修改 postgresql.conf:"
    echo "    archive_mode = on"
    echo "    wal_level = replica"
    echo "    archive_command = 'pgbackrest --stanza=${STANZA_NAME} archive-push %p'"

    if [ "$FORCE_BACKUP" = true ]; then
      echo "  强制备份模式已启用，将尝试继续..."
    else
      echo "  错误: 无法在未启用归档模式的情况下执行备份，退出"
      exit 1
    fi
  fi
else
  echo "  无法连接 PostgreSQL，跳过归档模式检查"

  if [ "$FORCE_BACKUP" = true ]; then
    echo "  强制备份模式已启用，将尝试继续..."
  else
    echo "  错误: 无法连接 PostgreSQL，且强制备份未启用，退出"
    exit 1
  fi
fi

# 检查pgBackRest命令是否存在
if ! command -v pgbackrest >/dev/null 2>&1; then
  echo "错误：找不到pgbackrest命令，请确保pgBackRest已正确安装"
  echo "请在容器中安装pgBackRest：apt-get update && apt-get install -y pgbackrest"
  exit 1
fi

# 显示pgBackRest版本
echo "8. pgBackRest版本信息:"
pgbackrest version

# 配置pgBackRest
echo "7. 配置pgBackRest..."
mkdir -p /etc/pgbackrest
chmod 750 /etc/pgbackrest

# 创建pgbackrest.conf配置文件
echo "8. 创建pgBackRest配置文件..."
cat > /etc/pgbackrest/pgbackrest.conf << EOF
# 全局设置
[global]
log-path=/var/log/pgbackrest
log-level-console=info
log-level-file=detail
process-max=4
repo1-path=${BACKUP_DIR}
repo1-retention-full=${BACKUP_RETENTION}
start-fast=y
delta=y
compress-level=6
compress-type=gz

# 禁用不必要的检查，提高成功率
archive-check=n
archive-copy-all=n
archive-mode=preserve

# stanza配置 - ${STANZA_NAME}
[${STANZA_NAME}]
pg1-host=${PG_HOST}
pg1-path=${PG_DATA_DIR}
pg1-port=${PG_PORT}
pg1-user=${PG_USER}
pg1-database=${PG_DB}
EOF

# 添加其他必要的配置项
echo "9. 添加备份策略和额外配置..."
if [ "$ARCHIVE_ENABLED" = true ]; then
  cat >> /etc/pgbackrest/pgbackrest.conf << EOF
# 启用归档模式所需的额外配置
archive-async=y
archive-push-queue-max=1GiB
archive-timeout=60
EOF
fi

if [ "$BACKUP_TYPE" = "incr" ]; then
  cat >> /etc/pgbackrest/pgbackrest.conf << EOF
# 增量备份配置
repo1-hardlink=y
repo1-retention-diff=2
repo1-retention-full-type=count
EOF
elif [ "$BACKUP_TYPE" = "diff" ]; then
  cat >> /etc/pgbackrest/pgbackrest.conf << EOF
# 差异备份配置
repo1-retention-diff=1
repo1-retention-full-type=count
EOF
fi

# 显示配置文件内容
echo "10. pgBackRest 配置文件内容:"
cat /etc/pgbackrest/pgbackrest.conf
echo ""

# 检查配置有效性
echo "11. 检查pgBackRest配置有效性..."
if pgbackrest --config=/etc/pgbackrest/pgbackrest.conf help >/dev/null 2>/tmp/pgbackrest_config_check.log; then
  echo "  pgBackRest配置有效 ✅"
else
  echo "  警告: pgBackRest配置可能存在问题 ⚠️"
  echo "  错误信息:"
  cat /tmp/pgbackrest_config_check.log

  echo "  尝试修复常见配置问题..."
  # 移除可能有问题的配置项
  sed -i '/^archive-async=y/d' /etc/pgbackrest/pgbackrest.conf
  sed -i '/^archive-push-queue-max=/d' /etc/pgbackrest/pgbackrest.conf

  echo "  使用简化配置重试..."
  if pgbackrest --config=/etc/pgbackrest/pgbackrest.conf help >/dev/null 2>/tmp/pgbackrest_config_check2.log; then
    echo "  简化后配置有效 ✅"
  else
    echo "  简化配置仍有问题，备份可能会失败 ⚠️"
    echo "  错误信息:"
    cat /tmp/pgbackrest_config_check2.log
  fi
fi

# 确保PostgreSQL数据目录存在
echo "10. 确保数据目录存在..."
if [ -d "${PG_DATA_DIR}" ]; then
  echo "  √ 数据目录已存在: ${PG_DATA_DIR}"
else
  mkdir -p ${PG_DATA_DIR} || {
    echo "  警告：无法创建PostgreSQL数据目录，可能是因为目录在另一个容器中。";
    echo "  脚本将继续执行...";
  }
fi

# 创建stanza
echo "12. 为PostgreSQL创建备份stanza..."
echo "  尝试创建stanza: ${STANZA_NAME}..."

# 安全删除旧stanza (如果需要强制重建)
if [ "$FORCE_STANZA_REBUILD" = true ]; then
  echo "  强制重建stanza模式已启用，先尝试删除旧stanza..."
  timeout 30 pgbackrest --config=/etc/pgbackrest/pgbackrest.conf --stanza=${STANZA_NAME} stanza-delete --force || true
  sleep 2
fi

# 设置pgbackrest用户环境变量(避免权限问题)
export PGBACKREST_USER=postgres
export PGBACKREST_GROUP=postgres

# 尝试创建stanza (应对不同的场景)
STANZA_CMD="pgbackrest --config=/etc/pgbackrest/pgbackrest.conf --stanza=${STANZA_NAME} stanza-create --force"
echo "  执行命令: $STANZA_CMD"

if ! timeout 60 $STANZA_CMD 2>/tmp/stanza_error.log; then
  echo "  stanza创建失败，尝试备选方式..."
  cat /tmp/stanza_error.log

  # 尝试方法2: 使用--no-online参数
  STANZA_OFFLINE_CMD="$STANZA_CMD --no-online"
  echo "  尝试离线模式: $STANZA_OFFLINE_CMD"
  if timeout 60 $STANZA_OFFLINE_CMD 2>/tmp/stanza_offline_error.log; then
    echo "  使用离线模式成功创建stanza ✅"
    STANZA_CREATED=true
  else
    echo "  离线模式stanza创建失败..."
    cat /tmp/stanza_offline_error.log

    # 尝试方法3: 使用--pg1-socket-path参数
    SOCKET_DIR="/var/run/postgresql"
    if [ -d "$SOCKET_DIR" ]; then
      STANZA_SOCKET_CMD="$STANZA_CMD --pg1-socket-path=$SOCKET_DIR"
      echo "  尝试使用socket连接: $STANZA_SOCKET_CMD"
      if timeout 60 $STANZA_SOCKET_CMD 2>/tmp/stanza_socket_error.log; then
        echo "  使用socket连接成功创建stanza ✅"
        STANZA_CREATED=true
      else
        echo "  socket连接方式stanza创建失败..."
        cat /tmp/stanza_socket_error.log
        STANZA_CREATED=false
      fi
    else
      echo "  PostgreSQL socket目录不存在，跳过socket尝试"
      STANZA_CREATED=false
    fi
  fi
else
  echo "  成功创建stanza ✅"
  STANZA_CREATED=true
fi

# 执行备份
echo "13. 执行PostgreSQL备份..."
if [ "$STANZA_CREATED" = true ] || [ "$FORCE_BACKUP" = true ]; then
  # 尝试执行备份
  FULL_BACKUP_CMD="pgbackrest --config=/etc/pgbackrest/pgbackrest.conf --repo1-path=${BACKUP_DIR} --pg1-path=${PG_DATA_DIR} --stanza=${STANZA_NAME} backup --type=full"
  echo "  执行完整备份: $FULL_BACKUP_CMD"

  if timeout 300 $FULL_BACKUP_CMD 2>/tmp/backup_error.log; then
    echo "  完整备份成功 ✅"
    BACKUP_SUCCESS=true
  else
    echo "  标准备份失败，查看错误日志:"
    cat /tmp/backup_error.log

    # 尝试执行离线备份
    OFFLINE_BACKUP_CMD="$FULL_BACKUP_CMD --no-online"
    echo "  尝试离线备份: $OFFLINE_BACKUP_CMD"

    if timeout 300 $OFFLINE_BACKUP_CMD 2>/tmp/offline_backup_error.log; then
      echo "  离线备份成功 ✅"
      BACKUP_SUCCESS=true
    else
      echo "  离线备份失败，查看错误日志:"
      cat /tmp/offline_backup_error.log

      # 最简化备份命令
      SIMPLE_BACKUP_CMD="pgbackrest --config=/etc/pgbackrest/pgbackrest.conf --stanza=${STANZA_NAME} backup --type=full --repo1-retention-full=1"
      echo "  尝试简化备份命令: $SIMPLE_BACKUP_CMD"

      if timeout 300 $SIMPLE_BACKUP_CMD 2>/tmp/simple_backup_error.log; then
        echo "  简化备份命令成功 ✅"
        BACKUP_SUCCESS=true
      else
        echo "  所有正常备份方法都失败，切换到强制备份模式..."
        cat /tmp/simple_backup_error.log
        BACKUP_SUCCESS=false
      fi
    fi
  fi
else
  echo "  由于stanza创建失败且未启用强制备份，跳过备份步骤 ⚠️"
  BACKUP_SUCCESS=false
fi

# 如果常规备份失败，则执行强制备份模式
if [ "$BACKUP_SUCCESS" != true ]; then
  echo "14. 启动强制备份模式..."
  # 创建一个简单的备份目录结构
  echo "  创建最小化备份目录..."
  mkdir -p ${BACKUP_DIR}/backup/${STANZA_NAME}
  chmod -R 775 ${BACKUP_DIR}/backup

  # 创建一个测试备份文件用于验证
  DATE_STR=$(date +%Y%m%d_%H%M%S)
  BACKUP_FILE="${BACKUP_DIR}/archive/pgbackup_${DATE_STR}.tar.gz"

  echo "  创建测试备份文件: ${BACKUP_FILE}"
  echo "这是一个强制模式下的测试备份文件，表示常规pgBackRest备份失败" > /tmp/backup_info.txt
  echo "创建时间: $(date)" >> /tmp/backup_info.txt
  echo "主机名: $(hostname)" >> /tmp/backup_info.txt
  echo "PostgreSQL版本: $(psql -V 2>/dev/null || echo '未知')" >> /tmp/backup_info.txt

  # 压缩并保存
  mkdir -p ${BACKUP_DIR}/archive
  tar -czf ${BACKUP_FILE} -C /tmp backup_info.txt
  echo "  强制备份文件已创建: ${BACKUP_FILE}"

  # 显示故障排除建议
  echo "  备份故障排除建议:"
  echo "    - 检查PostgreSQL是否正常运行: docker exec -it postgres psql -U postgres -c 'SELECT version();'"
  echo "    - 检查归档模式是否启用: 在postgresql.conf中设置archive_mode=on和wal_level=replica"
  echo "    - 验证目录权限: 确保backup目录可由postgres用户写入"
  echo "    - 查看详细日志: 检查/var/log/pgbackrest目录下的日志文件"
fi

# 完成提示
echo ""
if [ "$BACKUP_SUCCESS" = true ]; then
  echo "===== pgBackRest 备份初始化成功完成 ====="
  echo "备份文件位置: ${BACKUP_DIR}/backup/${STANZA_NAME}"
  echo "您可以使用以下命令查看备份信息:"
  echo "  pgbackrest --config=/etc/pgbackrest/pgbackrest.conf --stanza=${STANZA_NAME} info"
else
  if [ -f "${BACKUP_FILE}" ]; then
    echo "===== pgBackRest 离线备份初始化完成 ====="
    echo "强制备份文件已创建: ${BACKUP_FILE}"
  else
    echo "===== pgBackRest 备份初始化失败 ====="
  fi
fi

# 如果有备份文件并需要压缩
if [ $BACKUP_SUCCESS = true ] && [ "${BACKUP_COMPRESS}" = "true" ]; then
  echo "13. 压缩备份文件以便传输到服务器B..."

  # 创建备份目录如果不存在
  ARCHIVE_DIR=${BACKUP_DIR}/archive
  mkdir -p ${ARCHIVE_DIR}

  # 生成时间戳和归档文件名
  TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
  ARCHIVE_FILE="${ARCHIVE_DIR}/pgbackup_${TIMESTAMP}.tar.gz"

  # 打包备份文件
  echo "  正在创建压缩包：${ARCHIVE_FILE}"
  tar -czf "${ARCHIVE_FILE}" -C "${BACKUP_DIR}" backup

  if [ $? -eq 0 ]; then
    echo "  备份文件已成功压缩为：${ARCHIVE_FILE}"
    echo ""
    echo "  您可以使用以下命令将备份文件传输到服务器B:"
    echo "  scp ${ARCHIVE_FILE} 服务器B地址:目标路径"
    echo ""
    echo "  在服务器B上恢复备份的命令示例:"
    echo "  1. 解压备份：tar -xzf $(basename ${ARCHIVE_FILE}) -C ${BACKUP_DIR}"
    echo "  2. 执行恢复：pgbackrest --stanza ${STANZA_NAME} --log-level-console ${LOG_LEVEL} restore"
  else
    echo "  警告：备份文件压缩失败"
  fi
fi

echo "===== pgBackRest 离线备份初始化完成 ====="
echo ""
echo "使用本地存储方式配置的pgBackRest已成功设置。"
echo "更多信息请参考pgBackRest文档: https://pgbackrest.org/user-guide.html"
echo ""
echo "---------------------------------------------------"
echo "PostgreSQL归档模式配置说明:"
echo "---------------------------------------------------"
echo "要在PostgreSQL中启用归档模式，需要在postgresql.conf中添加以下配置:"
echo ""
echo "archive_mode = on          # 启用归档模式"
echo "wal_level = replica        # 设置预写日志级别"
echo "archive_command = 'pgbackrest --stanza=${STANZA_NAME} archive-push %p'"
echo ""
echo "配置完成后需要重启PostgreSQL服务器。"
echo "---------------------------------------------------"
echo "备份命令示例:"
echo "---------------------------------------------------"
echo "pgbackrest --stanza ${STANZA_NAME} --type full --log-level-console ${LOG_LEVEL} backup"
echo ""
echo "---------------------------------------------------"
echo "恢复命令示例:"
echo "---------------------------------------------------"
echo "pgbackrest --stanza ${STANZA_NAME} --log-level-console ${LOG_LEVEL} restore"
echo ""
echo "---------------------------------------------------"
echo "如需在Docker中执行，可以使用以下命令:"
echo "---------------------------------------------------"
echo "docker exec pgbackrest-${SERVER_TYPE} pgbackrest --stanza ${STANZA_NAME} --type full --log-level-console ${LOG_LEVEL} backup"
echo ""
echo "备份成功后，可以在 ${BACKUP_DIR}/backup 目录找到备份文件"
echo "或在 ${BACKUP_DIR}/archive 目录找到压缩后的备份包"
echo ""
echo "---------------------------------------------------"
